const canvas = document.querySelector('.fireworks');
const ctx = canvas.getContext('2d');
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;

// 粒子数组
const particles = [];

class Particle {
  constructor(x, y, color) {
    this.x = x;
    this.y = y;
    this.color = color;
    this.size = Math.random() * 4 + 1;
    this.speedX = Math.random() * 4 - 2;
    this.speedY = Math.random() * 4 - 2;
    this.opacity = 1;
    this.life = 0;
  }

  update() {
    this.x += this.speedX;
    this.y += this.speedY;
    this.opacity -= 0.02;
    this.life++;
  }

  draw() {
    ctx.globalAlpha = this.opacity;
    ctx.beginPath();
    ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
    ctx.fillStyle = this.color;
    ctx.fill();
  }
}

function createFireworks() {
  const colors = ['#ffd700', '#ff4500', '#ff6347', '#ff8c00', '#ff1493'];
  const x = Math.random() * canvas.width;
  const y = Math.random() * canvas.height * 0.5; // 控制烟花的高度
  const numberOfParticles = Math.random() * 20 + 50;

  for (let i = 0; i < numberOfParticles; i++) {
    const color = colors[Math.floor(Math.random() * colors.length)];
    particles.push(new Particle(x, y, color));
  }
}

function animate() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  particles.forEach((particle, index) => {
    particle.update();
    particle.draw();

    if (particle.opacity <= 0) {
      particles.splice(index, 1);
    }
  });

  requestAnimationFrame(animate);
}

// 祝福语数组
const blessings = [
  '祝愿新年喜气洋洋，身体健康，阖家幸福！',
  '愿新年平安吉祥，财源广进！',
  '新的一年，祝您梦想成真，幸福美满！',
  '愿您心想事成，笑口常开！',
  '新年好，愿好运天天伴随您！',
  '祝福新的一年，生活甜甜蜜蜜！',
  '新年到，愿您事业蒸蒸日上！',
  '愿幸福陪伴您左右，新年快乐！',
  '新年好，愿健康和快乐永远伴您！',
  '祝您福星高照，步步高升！'
];

// 随机设置祝福语
function setRandomBlessing() {
  const randomIndex = Math.floor(Math.random() * blessings.length);
  const blessingElement = document.getElementById('blessing');
  blessingElement.innerHTML = blessings[randomIndex];
}

// 页面加载时自动显示烟花和随机祝福语
window.onload = () => {
  setRandomBlessing();
  createFireworks();
  setInterval(createFireworks, 500); // 每500毫秒生成新的烟花
  animate(); // 启动动画
}

